/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : idpw.js                                 
*  Description  : 아이디, 비밀번호 찾기 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-04-18  김효순    Created.
*  2012-04-18  김효순    Complete.
*  2012-05-23  김효순    아이디찾기 핸드폰 부분 추가
*******************************************************************************/

jQuery(function($){
	if(_util.getCookie("member_id")){
		document.location.href = _ROOT + "/index";
	}
	
	// 아이디 찾기
	function clsId()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOEMAIL": "이메일 주소를 입력해 주십시요."
			, "_NOHP": "핸드폰 번호를 입력해 주십시요."
		}
		this.errMsg = null;
		this.$errObj = null;
		this.resData = null;
		
		this._init();
	}
	
	clsId.prototype = {
		_init: function(){
			$layer = $("div#container section.member:eq(0)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			var p = ["010", "011", "016", "017", "018", "019"];
			var e = _gf.getEmailList();
			
			$layer.find("select:eq(0)").empty();
			$layer.find("select:eq(0)").append("<option value=''>직접입력</option>");
			for(var i=0;i<e.length;i++){
				$layer.find("select:eq(0)").append("<option value='" + e[i]["이메일"] + "'>" + e[i]["텍스트"] + "</option>");
			}
			
			$layer.find("select:eq(1)").empty();
			for(var i=0;i<p.length;i++){
				$layer.find("select:eq(1)").append("<option value='" + p[i] + "'>" + p[i] + "</option>");
			}
			
			//$layer.find("select:eq(1)").html("<option value='010'>010</option>");
		}
		, $getSelect: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.find("select");
		}
		, $getBtnSubmit: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.find("a").has("img[alt='아이디 찾기']");
		}
		, getError: function(){
			if(!this.errMsg) return null;
			else return this._ERRMSG[this.errMsg];
		}
		, setValue: function(){
			var $layer = this._HTMLDom["$layer"];
			var $email = $layer.find(":text:eq(1)");
			
			$email.val($layer.find("option:selected").val());
		}
		, isValid: function(){
			var $layer = this._HTMLDom["$layer"];
			var $email1 = $layer.find(":text:eq(0)");
			var $email2 = $layer.find(":text:eq(1)");
			var $hp1 = $layer.find("input:text:eq(2)");
			var $hp2 = $layer.find("input:text:eq(3)");
			
			this.errMsg = null;
			this.$errObj = null;
			
			if(!($email1.val() && $email2.val())){
				this.errMsg = "_NOEMAIL";
				
				if(!$email1.val()) this.$errObj = $email1;
				else if(!$emial2.val()) this.$errObj = $email2;
				return false;
			}else if(!($hp1.val() && $hp2.val())){
				this.errMsg = "_NOHP";
				
				if(!$hp1.val()) this.$errObj = $hp1;
				else if(!$hp2.val()) this.$errObj = $hp2;
				return false;
			}
			
			return true;
		}
		, focus: function(){
			var $layer = this._HTMLDom["$layer"];
			
			if(this.$errObj == null) $layer.find("input:text:first").trigger("focus");
			else{
				this.$errObj.trigger("focus");
			}
		}
		, setResult: function(ret){
			this.resData = ret;
		}
		, getResult: function(){
			return this.resData;
		}
		, getEmailValue: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.find("input:text:eq(0)").val() + "@" + $layer.find("input:text:eq(1)").val();
		}
		, getHpValue: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.find("select:eq(1) option:selected").text() + "-" + $layer.find("input:text:eq(2)").val() + "-" + $layer.find("input:text:eq(3)").val();
		}
	}
	
	// 비밀번호 찾기
	function clsPw()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOID": "아이디를 입력해 주십시요."
			, "_NOEMAIL": "이메일 주소를 입력해 주십시요."
		}
		this.errMsg = null;
		this.$errObj = null;
		this.resData = null;
		
		this._init();
	}
	
	clsPw.prototype = {
		_init: function(){
			$layer = $("div#container section.member:eq(1)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			var e = _gf.getEmailList();
			
			$layer.find("select:eq(0)").empty();
			$layer.find("select:eq(0)").append("<option value=''>직접입력</option>");
			for(var i=0;i<e.length;i++){
				$layer.find("select:eq(0)").append("<option value='" + e[i]["이메일"] + "'>" + e[i]["텍스트"] + "</option>");
			}
		}
		, $getSelect: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.find("select");
		}
		, $getBtnSubmit: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.find("a").has("img[alt='비밀번호 찾기']");
		}
		, getError: function(){
			if(!this.errMsg) return null;
			else return this._ERRMSG[this.errMsg];
		}
		, setValue: function(){
			var $layer = this._HTMLDom["$layer"];
			var $email = $layer.find(":text:eq(2)");
			
			$email.val($layer.find("option:selected").val());
		}
		, isValid: function(){
			var $layer = this._HTMLDom["$layer"];
			var $id = $layer.find("input:text:eq(0)");
			var $email1 = $layer.find(":text:eq(1)");
			var $email2 = $layer.find(":text:eq(2)");
			
			this.errMsg = null;
			this.$errObj = null;
			
			if(!$id.val()){
				this.errMsg = "_NOID";
				this.$errObj = $id;
				return false;
			}else if(!($email1.val() && $email2.val())){
				this.errMsg = "_NOEMAIL";
				
				if(!$email1.val()) this.$errObj = $email1;
				else if(!$email2.val()) this.$errObj = $email2;
				
				return false;
			}
			
			return true;
		}
		, focus: function(){
			var $layer = this._HTMLDom["$layer"];
			
			if(this.$errObj == null) $layer.find("input:text:first").trigger("focus");
			else{
				this.$errObj.trigger("focus");
			}
		}
		, setResult: function(ret){
			this.resData = ret;
		}
		, getResult: function(){
			return this.resData;
		}
		, getIdValue: function(){
			return this._HTMLDom["$layer"].find("input:text:eq(0)").val();
		}
		, getEmailValue: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.find("input:text:eq(1)").val() + "@" + $layer.find("input:text:eq(2)").val();
		}
	}
	
	function clsComm()
	{
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			
		}
		, idSearchSubmit: function(arg){
			var thisClass = this;
			
//			arg = {
//				"이메일": null
//				, "핸드폰": null
//			}
			
			//ajax
			var serviceName = "findId";
			document.objGlobal.ajax({
				"sid": "US001"
				, "servicename": serviceName
			}, {
				"email": arg["이메일"]
				, "phonenum": arg["핸드폰"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
				}
				
				var infoData = {};
				
				var items = ret["msp"]["contents"]["items"];
				
				if(items){
					var obj = items;
					infoData["아이디"] = obj["userId"];
					
					res["메시지"] = _gf.getResultMsg(ret, [infoData["아이디"]]);
				}
				
				document.objControl.finishIdSearchSubmit(res, infoData, arg);
			});
		}
		, pwSearchSubmit: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "findPassword";
			document.objGlobal.ajax({
				"sid": "US001"
				, "servicename": serviceName
			}, {
				"userId": arg["아이디"]
				, "email": arg["이메일"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				if(res["결과코드"] == "00000"){
					res["메시지"] = _gf.getResultMsg(ret, [arg["이메일"]]);
				}
				
				document.objControl.finishPwSearchSubmit(res, null, arg);
			});
		}
	}
	
	function clsControl()
	{
		this.objComm = null;
		this.objId = null;
		this.objPw = null;
		this.objModalLoading = null;
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			this.objId = new clsId();
			this.objPw = new clsPw();
			
			document.objGlobal.objModalLoading.setText("searching...");
			
			this._bind();
		}
		, _bind: function(){
			var thisClass = this;
			
			// 아이디 찾기 이메일 셀렉트박스
			this.objId.$getSelect().change(function(){
				thisClass.objId.setValue();
			});
			
			// 비밀번호 찾기 이메일 셀렉트박스
			this.objPw.$getSelect().change(function(){
				thisClass.objPw.setValue();
			});
			
			// 아이디 찾기
			this.objId.$getBtnSubmit().click(function(){
				if(!thisClass.objId.isValid()){
					alert(thisClass.objId.getError());
					thisClass.objId.focus();
					return;
				}
				
				//_gf.spinStart();
				thisClass.objComm.idSearchSubmit({
					"이메일": thisClass.objId.getEmailValue()
					, "핸드폰": thisClass.objId.getHpValue()
				});
			});
			
			// 비밀번호 찾기
			this.objPw.$getBtnSubmit().click(function(){
				if(!thisClass.objPw.isValid()){
					alert(thisClass.objPw.getError());
					thisClass.objPw.focus();
					return;
				}
				
				document.objGlobal.spinStart();
				thisClass.objComm.pwSearchSubmit({
					"아이디": thisClass.objPw.getIdValue()
					, "이메일": thisClass.objPw.getEmailValue()
				});
			});
			
			$("div#top_menu div.top_menu").on("click", "a", function(){
				var tx = $(this).text();
				
				switch(tx){
					case "로그인":
						_gf.goLogin();
						break;
					case "골프인홈":
						thisClass.goHomePage();
						break;
				}
			});
			
			$("div#container").on("click", "img[alt='고객센터 바로가기']", function(){
				var alt = $(this).attr("alt");
				switch(alt){
					case "고객센터 바로가기":
						thisClass.goCustomerPage();
						break;
				}
			});
		}
		, getIdResult: function(){
			return this.objId.getResult();
		}
		, getPwResult: function(){
			return this.objPw.getResult();
		}
		// 아이디 찾기 완료 후
		, finishIdSearchSubmit: function(ret, infoData, arg){
			_gf.spinEnd();
			
			ret["아이디"] = infoData["아이디"];
			
			this.objId.setResult(ret);
			
			var winPopIdPw = window.open("../popup/pop_idpw.html?q_type=id", "winPopIdPw", "width=300, height=100, scrollbars=0");
			if(winPopIdPw) winPopIdPw.focus();
		}
		// 비밀번호 찾기 완료 후
		, finishPwSearchSubmit: function(ret, infoData, arg){
			document.objGlobal.spinEnd();
			
			this.objPw.setResult(ret);
			
			var winPopIdPw = window.open("../popup/pop_idpw.html?q_type=pw", "winPopIdPw", "width=300, height=100, scrollbars=0");
			if(winPopIdPw) winPopIdPw.focus();
		}
		, goLogin: function(w){
			if(w) w.close();
			
			var userId = this.getIdResult()["아이디"];
			
			_gf.goLogin(null, "q_userid=" + userId);
		}
		, goHomePage: function(){
			var szLink = _ROOT + "/index";
			document.location.href = szLink;
		}
		// 고객센터 바로가기
		, goCustomerPage: function(){
			var szLink = _ROOT + "/launge/customer.html";
			document.location.href = szLink;
		}
	}
	
	document.objControl = new clsControl();
});